﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeaBattle.Domain.Entities
{
    public class Fleet
    {
        private readonly byte MaxShipCount;
        private readonly byte MaxPartCount;

        private Dictionary<Ship, ShipOrientation> ships = new Dictionary<Ship, ShipOrientation>();

        public Fleet(byte maxShipCount, byte maxPartCount)
        {
            this.MaxShipCount = maxShipCount;
            this.MaxPartCount = maxPartCount;
        }

        public int Id { get; set; }

        private bool CanAddShip(Ship ship)
        {
            if (ships.Count + 1 > MaxShipCount)
                return false;
          /*  int Count = 0;
            foreach (Ship x in ships)
                Count += x.PartsCount;*/
            var PartsCount = (from s in ships select s.Key.PartsCount).Sum();
            if (PartsCount + ship.PartsCount > MaxPartCount)
                return false;
            return true;
        }

        public void AddShip(Ship ship,ShipOrientation orientation)
        {
            if (CanAddShip(ship))
            {
                ships.Add(ship, orientation);
            }
        }

        public bool RemoveShip(Ship ship)
        {
            if (ships.ContainsKey(ship))
                ships.Remove(ship);
            else
                return false;
            return true;
        }

        public bool RemoveShip(int id_ship)
        {
            if (HasShip(id_ship))
            {
                var dieShip = (from s in ships.Keys where s.Id == id_ship select s).First();
                RemoveShip(dieShip);
            }
            else
                return false;
            return true;
        }

        public bool HasShip(int id_ship)
        {
            if ((from s in ships.Keys where s.Id == id_ship select s).Count() != 0)
                return true;
            else
                return false;
        }

        public void ResetFleet()
        {
                ships.Clear();
        }

    }
}
